package org.springleaf.core.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springleaf.core.json.IJsonObject;
import org.springleaf.utils.Obj2MapUtils;

/**
 * 分页结果对象
 * @author leaf
 * @param <T> 泛型
 */
public class PageList<T> implements IJsonObject{
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 数据库总的记录行数
	 */
	private int totalCount = 0;
	/**
	 * 当前查询出来的记录行数
	 */
	private int count = 0;
	/**
	 * 查出来的结果
	 */
	private List<T> result = new ArrayList<T>();

	/**
	 * 当前是第几页
	 */
	private int currentPageNo = 0;

	/**
	 * 每一页大小
	 */
	private int pageSize = 1;
	

	/**
	 * @param totalCount 数据库复合条件的总记录数
	 * @param result 当前查询出来的数据集合
	 */
	public PageList(int totalCount, List<T> result) {
		
		this.totalCount = totalCount;
		this.result = result;
		if(this.result != null)
			this.count = this.result.size();
		
	}

	/**
	 * 得到当前数据能分多少页
	 */
	public int getPages(){
		if(this.pageSize != 0)
			return this.totalCount / this.pageSize;
		return 0;
	}
	

	public int getTotalCount() {
		return totalCount;
	}

	public int getCount() {
		return count;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public Object toJSonObject() {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", totalCount);
		map.put("rows", Obj2MapUtils.toMapList(result));
		return map;
	}
	
}
